﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class showResult : System.Web.UI.Page
{
    DB DBManager = new DB();
    protected void Page_Load(object sender, EventArgs e)
    {
        string width = "500px";
        //int id = Convert.ToInt32(Request.QueryString["id"].ToString());
        string sqlGetId = "select id from voteMaster";
        string sqlGetSize = "select count(*)from voteMaster";
        int length = Convert.ToInt32(DBManager.executeGetReturn(sqlGetSize));
        int[] voteIds = new int[length];
        SqlDataReader sdr1 = DBManager.getSdr(sqlGetId);
        int j = 0;
        while (sdr1.Read())
        {
            voteIds[j] = sdr1.GetInt32(0);
            j = j + 1;
        }
        sdr1.Close();
        for (int s = 0; s < length; s++)
        {
            string sqlvoteTit = "select voteTitle from voteMaster where id=" + voteIds[s];
            string voteTit = Convert.ToString(DBManager.executeGetReturn(sqlvoteTit));
            string getSum = "select sum(voteNum) from voteDetails where id=" + voteIds[s];
            int voteSum = Convert.ToInt32(DBManager.executeGetReturn(getSum));

            string sqlvoteDet = "select voteItem,voteNum from voteDetails where id=" + voteIds[s];
            SqlDataReader sdr2 = DBManager.getSdr(sqlvoteDet);
            Response.Write("<table style='width:" + width + "'><tr><td style='border: #4a95c9 1px solid;background-color: #aacc00;padding:5px 5px 5px 5px;'>");
            Response.Write("<strong>" + voteTit + "</strong><br />");
            int i = 0;
            while (sdr2.Read())
            {
                int m = sdr2.GetInt32(1);
                Response.Write("<br/><span style='font-size:12px;'>" + (i + 1) + "&nbsp;&nbsp;" + sdr2.GetString(0) + "</a>&nbsp;&nbsp;票数:" + m + "百分比:");

                if (voteSum != 0)
                {
                    Response.Write(Convert.ToSingle(m) / Convert.ToSingle(voteSum) * 100 + "%");
                }
                else
                {
                    Response.Write("0%");
                }
                int q = i;
                if (i > 8) q = 8;
                int clo = 9900 - 1100 * q;
                Response.Write("</span><br/><br/> <div style='height:10px; background-color:White;border:1px solid #ccc;'><div style='height:10px; background-color:#ff" + Convert.ToString(clo) + "; width:");

                if (voteSum != 0)
                {
                    Response.Write(Convert.ToInt32(Convert.ToSingle(m) / Convert.ToSingle(voteSum) * 100) + "%");
                }
                else
                {
                    Response.Write("0");
                }
                Response.Write("'></div></div>");
                i = i + 1;
            }
            Response.Write("<br /><strong>总票数</strong>: " + voteSum + "");
            Response.Write("</td></tr></table>");
            sdr2.Close();
            DBManager.clear();
        }
        Button btnToVote = new Button();
        btnToVote.ID = "btnToVote";
        btnToVote.Text = "返回投票页面";
        btnToVote.Click += new EventHandler(btnToVote_Click);
        form1.Controls.Add(btnToVote);
    }

    protected void btnToVote_Click(object sender, EventArgs e)
    {
        Response.Redirect("vote.aspx");
    }
}
